package com.fc.example;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.anythink.core.api.ATAdConst;
import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.ATAdSourceStatusListener;
import com.anythink.core.api.AdError;
import com.anythink.nativead.api.ATNative;
import com.anythink.nativead.api.ATNativeAdView;
import com.anythink.nativead.api.ATNativeDislikeListener;
import com.anythink.nativead.api.ATNativeEventExListener;
import com.anythink.nativead.api.ATNativeNetworkListener;
import com.anythink.nativead.api.ATNativePrepareExInfo;
import com.anythink.nativead.api.ATNativePrepareInfo;
import com.anythink.nativead.api.ATNativeView;
import com.anythink.nativead.api.NativeAd;

import java.util.HashMap;
import java.util.Map;

public class FeedExpressActivity extends AppCompatActivity {

    private static final String TAG = "FeedExpressActivity";
    public String mMediaId; // 融合广告位

    private ATNativeView mATNativeView;

    private ATNative mATNative;
    private NativeAd mNativeAd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single);

        this.mMediaId = "b65a7a9b5b7fb8";

        ((TextView) findViewById(R.id.tv_media_id)).setText(getResources().getString(R.string.ad_mediation_id, mMediaId));

        // feed流广告容器
        mATNativeView = findViewById(R.id.native_ad_view);

        // 加载广告
        this.findViewById(R.id.bt_load).setOnClickListener(it -> loadAd());

        // 展示广告
        this.findViewById(R.id.bt_show).setOnClickListener(it -> showAd());

        initATNativeAd();
    }

    private void initATNativeAd() {
        mATNative = new ATNative(this, mMediaId, new ATNativeNetworkListener() {
            @Override
            public void onNativeAdLoaded() {
                Log.i("test", "onNativeAdLoaded");
                Toast.makeText(FeedExpressActivity.this,"加载成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNativeAdLoadFail(AdError adError) {
                Log.i("test", "onNativeAdLoadFail, " + adError.getFullErrorInfo());
                Toast.makeText(FeedExpressActivity.this,"加载失败:" + adError.toString(),Toast.LENGTH_SHORT).show();
            }
        });
        mATNative.setAdSourceStatusListener(new ATAdSourceStatusListenerImpl());
    }

    private void loadAd() {
        Toast.makeText(FeedExpressActivity.this,"开始加载",Toast.LENGTH_SHORT).show();
        Map<String, Object> localExtra = new HashMap<>();
//        localExtra.put(ATAdConst.KEY.AD_WIDTH, 350); // 单位是dp
//        localExtra.put(ATAdConst.KEY.AD_HEIGHT, 0); // 单位是dp

        mATNative.setLocalExtra(localExtra);
        mATNative.makeAdRequest();
    }

    // 在加载成功后展示广告
    private void showAd() {
        NativeAd nativeAd = mATNative.getNativeAd();
        if (nativeAd != null) {
            if (mNativeAd != null) {
                mNativeAd.destory();
            }
            mNativeAd = nativeAd;

            mNativeAd.setNativeEventListener(new ATNativeEventExListener() {
                @Override
                public void onDeeplinkCallback(ATNativeAdView view, ATAdInfo adInfo, boolean isSuccess) {
                    Log.i(TAG, "onDeeplinkCallback:" + adInfo.toString() + "--status:" + isSuccess);
                }

                @Override
                public void onAdImpressed(ATNativeAdView view, ATAdInfo entity) {
                    Log.i(TAG, "native ad onAdImpressed:\n" + entity.toString());
                }

                @Override
                public void onAdClicked(ATNativeAdView view, ATAdInfo entity) {
                    Log.i(TAG, "native ad onAdClicked:\n" + entity.toString());
                }

                @Override
                public void onAdVideoStart(ATNativeAdView view) {
                    Log.i(TAG, "native ad onAdVideoStart");
                }

                @Override
                public void onAdVideoEnd(ATNativeAdView view) {
                    Log.i(TAG, "native ad onAdVideoEnd");
                }

                @Override
                public void onAdVideoProgress(ATNativeAdView view, int progress) {
                    Log.i(TAG, "native ad onAdVideoProgress:" + progress);
                }
            });

            mNativeAd.setDislikeCallbackListener(new ATNativeDislikeListener() {
                @Override
                public void onAdCloseButtonClick(ATNativeAdView view, ATAdInfo entity) {
                    Log.i(TAG, "native ad onAdCloseButtonClick");
                }
            });

            mATNativeView.removeAllViews();

            ATNativePrepareInfo mNativePrepareInfo = null;

            try {
                mNativePrepareInfo = new ATNativePrepareExInfo();

                if (mNativeAd.isNativeExpress()) {
                    mNativeAd.renderAdContainer(mATNativeView, null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            mNativeAd.prepare(mATNativeView, mNativePrepareInfo);
            mATNativeView.setVisibility(View.VISIBLE);

        }
    }

    public static class ATAdSourceStatusListenerImpl implements ATAdSourceStatusListener {
        private final String TAG = getClass().getSimpleName();

        @Override
        public void onAdSourceBiddingAttempt(ATAdInfo adInfo) {
            Log.i(TAG, "onAdSourceBiddingAttempt: " + adInfo.toString());
        }

        @Override
        public void onAdSourceBiddingFilled(ATAdInfo adInfo) {
            Log.i(TAG, "onAdSourceBiddingFilled: " + adInfo.toString());
        }

        @Override
        public void onAdSourceBiddingFail(ATAdInfo adInfo, AdError adError) {
            Log.i(TAG, "onAdSourceBiddingFail Info: " + adInfo.toString());
            if (adError != null) {
                Log.i(TAG, "onAdSourceBiddingFail error: " + adError.getFullErrorInfo());
            }
        }

        @Override
        public void onAdSourceAttempt(ATAdInfo adInfo) {
            Log.i(TAG, "onAdSourceAttempt: " + adInfo.toString());
        }

        @Override
        public void onAdSourceLoadFilled(ATAdInfo adInfo) {
            Log.i(TAG, "onAdSourceLoadFilled: " + adInfo.toString());
        }

        @Override
        public void onAdSourceLoadFail(ATAdInfo adInfo, AdError adError) {
            Log.i(TAG, "onAdSourceLoadFail Info: " + adInfo.toString());
            Log.i(TAG, "onAdSourceLoadFail error: " + adError.getFullErrorInfo());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /** 6、在onDestroy中销毁广告 */
        if (mNativeAd != null) {
            mNativeAd.destory();
        }
    }
}